Binary search principle is very simple, but the boundary conditions prone to error, the death cycle, and so on, to be completely clear, you should understand thoroughly, we can use the first thought, and then use examples to verify, the following is my idea, if there are errors, please correct me. We first look at deformation one: if there are a lot of numbers to find, find the largest, so the largest must be at the far right, in order to be able to compare the left to two numbers, the end of th
The binary search principle is very simple, but the boundary conditions are prone to errors and endless loops. to thoroughly distinguish them, you must understand them thoroughly. You can first think about them and then use examples to verify them, the following is my idea. If there is any error, please correct it. Let's first look at deformation 1: if there are a lot of numbers to be searched, find the largest ones, so the biggest one must be at the rightmost. In order to compare the two number
[Java]Package Ceshi;Public class biSearch {/*** @ Param args*//*Semi-query-when the table is an ordered table, you can use semi-query;Basic Idea: in an ordered table, take the intermediate element as the comparison object. If the given value K is the same as the intermediate record keyword, the search is successful;If the given value K is less than the keyword of the intermediate record, continue searching in the left half of the table;If the given va
, this is a 2-point search question, but pay attention to the data of the next question !! The real number of 1e10 !! And the precision is 0.0001, so even if the data size is 2 pointsIs it a pity that the usual recursive method is... re... no way, it can only loop.The following is the recursive reCode: # Include Using namespace STD ; # Define POW (x) * (x) # define pow3 (x) (POW (x) * (x) # define pow4 (X) (POW (x) * POW (x )) Double Y = 0 ; Bool Doueql ( Double A , Double B ){ If ( FABS (
value to be found 3.beg for the lookup range start 4.last for the search range end/////////////////////////////////////////////////int Bisearch::bisearch (vectorint mid;//Middle Positionif (beg>last) {//beg start position is greater than last end position, currently only consider ascending statereturn-1;}while (BegMid = (beg+last)/2;if (X==numvec[mid]) {//x (key code) is exactly the same as the median (Num
}; // original array
Int B [N]; // an array used in dynamic planning. It is used to record intermediate results. The meaning is ambiguous. For more information, see the blog post.
Int Len; // indicates the number of elements in array B.
Int Lis (int * array, int N); // calculates the length of the longest incrementing sub-sequence and calculates the elements of array B. After array [] loops are completed, the length of B is Len.
Int bisearch
caller to inform the index range of the data source, and provides the logic for comparing the data at the specified index (as a delegate ).
In this way, algorithms only need to operate on indexes and are isolated from specific data sources. The comparison logic and can be customized by users.
Example:
Code
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->
Searchalgorithm. bisearchmatch match
=
Delegate
(
Int
Index){
Ret
binary tree. The first half is divided into the left subtree, and the second half is the right subtree. The number of searches by the half lookup method is exactly the number of layers where the value is located. In the case of equal probability
Log2 (n + 1)-1
The Code is as follows:
// Data is the array to be searched, x is the value of the Data to be searched, beg is the start of the search range, and last is the end of the search range// Non-Recursive MethodInt
the number of layers where the value is located. In the case of equal probability, aboutLOG2 (n+1)-1[CPP]View Plaincopy
Data is the array to find, X is the value to look for, beg is the start of the lookup range, and last is the end of the lookup range
Non-recursive method
int Bisearch (int data[], const int x, int beg, int last)
{
int mid; //Middle position
if (Beg > last)
{
return-1;
}
While (Beg
{
Mid = (b
method described in O (NlogN) algorithm 2 needs to traverse all dp [0] ~ When solving dp [I]. Dp [I-1], so can reduce each traversal?
In fact, when I is a little larger, j =, 2, and so on may not be able to meet the requirements of dp [j]> dp [I]-1, and it is useless to traverse them, after finding a j that meets both of the preceding conditions, we can determine that items smaller than dp [j] do not need to be considered. Therefore, we need to find items that meet val [j].
number of layers in which the value is located. In the case of equal probability, about
LOG2 (n+1)-1
Copy Code code as follows:
The data is the array to look for, X is the value to look for, beg is the start of the lookup range, and last is the lookup range terminated
Non-recursive method
int bisearch (int data[], const int x, int beg, int last)
{
int mid;//Middle Position
if (Beg > last)
{
return-1;
}
while (Beg {
Mid = (beg
, min; Int[] arr={3,2,1,6,9,5}; The initial value of Max=min=arr[0];//max and Min can be an array of any two values, but the following loop must start with subscript 0 (i=0) for (int i=1;iOperation Result:Binary (two-point search):
Example:
Class Bisearch {/* binary (binary) lookup: algorithm idea: requires sequence must be ordered (ascending/descending), because only in order to determine the scope of the data, through constant binary
binary tree. the first half is divided into the left subtree, and the second half is the right subtree. The number of searches by the half lookup method is exactly the number of layers where the value is located. In the case of equal probability
Log2 (n + 1)-1
The code is as follows:
// Data is the array to be searched, x is the value of the Data to be searched, beg is the start of the search range, and last is the end of the search range// Non-recursive methodInt
divided into the left subtree, and the second half is the right subtree. The number of searches by the half lookup method is exactly the number of layers where the value is located. In the case of equal probability
Log2 (n + 1)-1
Copy codeThe Code is as follows:// Data is the array to be searched, x is the value of the Data to be searched, beg is the start of the search range, and last is the end of the search range// Non-Recursive MethodInt BiSearch
Problem:Find an ordinal (dictionary-ordered) string array of elements in arr that are equal to the string V, and if more than one element satisfies this condition, returns the largest of the ordinal.int Bisearch (char** arr, int b, int e, char* v){There are two scenarios for the end of the loop:If the minindex is an even number minindex==maxindex;otherwise it is minindex==maxindex-1;while (minindex{Midindex = Minindex + (maxindex-minindex)/2;//not Use
1. BackgroundFor example, an integer x is a set of numbers in a sequence of columns sorted by size, and we want to find the index position of x in the sequence.For example, the sequence is arranged from small to large:-3,-2,0,4,5,7,12,64We want to find the position of the number 7, if it is a linear lookup, the time complexity is O (n), if you find with binary, the time complexity is O (log (n)), because each time the binary, the calculation of less than half, so take the logarithm.2. CodePackag
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.